﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using EBAYVN.Dao;
using EBAYVN.DOMAIN;
using EBAYVN.Util;
using EBAYVN.WEBADMIN.Common;
using EBAYVN.WEBADMIN.Shared;
using EBAYVN.WEBADMIN.UserControls;
using System.Xml;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;


namespace EBAYVN.WEBADMIN.Forms.BaoCao.DanhMuc
{
    public partial class LoaiHangHoa : Authentication
    {
        ReportClass rpDao = new ReportClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!Page.IsPostBack)
                {
                    LoadReferences();
                }
                else
                {
                    if (Session[Sessionkey.ReportSession] != null)
                        LayBaoCao((DataTable)Session[Sessionkey.ReportSession]);
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        #region Startup script registeration
        private void ShowError(string message, string controlId)
        {
            ((EOS)Page.Master).ShowError(message, controlId);
        }

        private void ShowError(string message)
        {
            ((EOS)Page.Master).ShowError(message);
        }

        private void ShowInfor(string message)
        {
            ((EOS)Page.Master).ShowInfor(message);
        }

        private void CloseWaitingDialog()
        {
            ((EOS)Page.Master).CloseWaitingDialog();
        }

        private void SetReadonly(string id, bool isReadonly)
        {
            ((EOS)Page.Master).SetReadonly(id, isReadonly);
        }

        private void SetControlValue(string id, string value)
        {
            ((EOS)Page.Master).SetControlValue(id, value);
        }

        private void HideDialog(string divId)
        {
            ((EOS)Page.Master).HideDialog(divId);
        }

        private void UnblockDialog(string divId)
        {
            ((EOS)Page.Master).UnblockDialog(divId);
        }

        #endregion

        private void LoadReferences()
        {
            txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            divReport.Visible = false;

            txtNguoiLap.Text = LoginInfo.NHANVIEN != null ? LoginInfo.NHANVIEN.HOTEN : "";
        }

        protected void btnBaoCao_Click(object sender, EventArgs e)
        {
            if (Session[Sessionkey.ReportSession] != null)
                Session[Sessionkey.ReportSession]=null;
            var TuNgay = DateTimeUtil.GetVietNamDate(txtTuNgay.Text.Trim());
            var DenNgay = DateTimeUtil.GetVietNamDate(txtDenNgay.Text.Trim());
            var dt = rpDao.LoaiHangHoa().Tables[0];
            LayBaoCao(dt);
            CloseWaitingDialog();
        }
        private void LayBaoCao(DataTable dt)
        {

            var rp = new ReportDocument();
            var path = Server.MapPath("../../../Reports/DanhMuc/LoaiHangHoa.rpt");
            rp.Load(path);
            var txtTuNgayrp = rp.ReportDefinition.ReportObjects["txtTuNgay"] as TextObject;
            if (txtTuNgayrp != null)
                txtTuNgayrp.Text = "Từ ngày " + txtTuNgay.Text.Trim() + " đến ngày " + txtDenNgay.Text.Trim();
            var txtTitle = rp.ReportDefinition.ReportObjects["txtTitle"] as TextObject;
            if (txtTitle != null)
                txtTitle.Text = "DANH SÁCH LOẠI HÀNG HÓA CỦA CÔNG TY";

            var txtNgayThangNam = rp.ReportDefinition.ReportObjects["txtNgayThangNam"] as TextObject;
            if (txtNgayThangNam != null)
                txtNgayThangNam.Text = rpDao.GetNgayThangNam();
            var txtNguoiLap1 = rp.ReportDefinition.ReportObjects["txtNguoiLap"] as TextObject;
            if (txtNguoiLap1 != null)
                txtNguoiLap1.Text = txtNguoiLap.Text.Trim();
            rp.SetDataSource(dt);
            rpViewer.ReportSource = rp;
            rpViewer.DataBind();
            divReport.Visible = true;
            Session[Sessionkey.ReportSession] = dt;
        }
    }
}